package service

import (
	__ "activity_srv/basic/proto"
	"activity_srv/handler/dao"
	"activity_srv/handler/models"
	"context"
	"errors"
	"time"
)

// 添加砍价活动
func (s *Serve) CreateCutActivity(_ context.Context, in *__.CreateCutActivityReq) (*__.CreateCutActivityResp, error) {
	id, err := dao.CreateCutActivity(in)
	if err != nil {
		return nil, errors.New("添加活动失败" + err.Error())
	}
	return &__.CreateCutActivityResp{Id: int64(id)}, nil
}

// 添加参加商品
func (s *Serve) AddCutGoods(_ context.Context, in *__.AddCutGoodsReq) (*__.AddCutGoodsResp, error) {
	id, err := dao.AddCutGoods(in)
	if err != nil {
		return nil, errors.New("添加商品失败" + err.Error())
	}
	return &__.AddCutGoodsResp{Id: int64(id)}, nil
}

// 用户参加砍价活动
func (s *Serve) UserJoinCutActivity(_ context.Context, in *__.UserJoinCutActivityReq) (*__.UserJoinCutActivityResp, error) {
	id, err := dao.UserJoinActivity(in)
	if err != nil {
		return nil, err
	}
	return &__.UserJoinCutActivityResp{Id: int64(id)}, nil
}

// 活动商品列表
func (s *Serve) CutGoodsList(_ context.Context, in *__.CutGoodsListReq) (*__.CutGoodsListResp, error) {
	list, err := dao.CutGoodsList(int(in.ActivityId))
	if err != nil {
		return nil, err
	}
	return &__.CutGoodsListResp{List: list}, nil
}

// 帮忙砍价
func (s *Serve) HelpCutPrice(_ context.Context, in *__.HelpCutPriceReq) (*__.HelpCutPriceResp, error) {
	price, err := dao.HelpCutPrice(in)
	if err != nil {
		return nil, err
	}
	return &__.HelpCutPriceResp{Price: float32(price)}, nil
}

// 生成邀请码
func (s *Serve) GetCutInvitationCode(_ context.Context, in *__.GetCutInvitationCodeReq) (*__.GetCutInvitationCodeResp, error) {
	res, _ := dao.GetCutInvitationCode(in.UserId, in.GoodsId, in.JoinId)
	return &__.GetCutInvitationCodeResp{InvitationCode: res}, nil
}

// 砍价商品详情
func (s *Serve) CutGoodsDetail(_ context.Context, in *__.CutGoodsDetailReq) (*__.CutGoodsDetailResp, error) {
	res, err := dao.CutGoodsDetail(int(in.Id))
	if err != nil {
		return nil, err
	}
	return &__.CutGoodsDetailResp{
		Id:          int64(res.Id),
		Title:       res.Title,
		GoodsName:   res.GoodsName,
		GoodsImg:    res.GoodsImg,
		Stock:       int64(res.Stock),
		Desc:        res.Desc,
		BasePrice:   float32(res.BasePrice),
		CutMaxPrice: float32(res.CutMaxPrice),
		CutMinPrice: float32(res.CutMinPrice),
		CutNum:      int64(res.CutNum),
		MinPrice:    float32(res.MinPrice),
		MinPerson:   int64(res.MinPerson),
		MaxPerson:   int64(res.MaxPerson),
		StartTime:   res.StartTime.Format(time.DateTime),
		StopTime:    res.StopTime.Format(time.DateTime),
		Image:       res.Image,
		MaxBuy:      int64(res.MaxBuy),
		MinBuy:      int64(res.MinBuy),
	}, nil
}

// 砍价活动列表
func (s *Serve) GetCutActivityList(_ context.Context, in *__.GetCutActivityListReq) (*__.GetCutActivityListResp, error) {
	list, err := dao.CutActivityList(int(in.Status))
	if err != nil {
		return nil, err
	}
	return &__.GetCutActivityListResp{List: list}, nil
}

// 活动详情
func (s *Serve) GetCutActivityDetail(_ context.Context, in *__.GetCutActivityDetailReq) (*__.GetCutActivityDetailResp, error) {
	a, err := dao.CutActivityDetail(in.ActivityId)
	if err != nil {
		return nil, err
	}
	return &__.GetCutActivityDetailResp{
		Id:        int64(a.Id),
		Title:     a.Title,
		Image:     a.Image,
		StartTime: a.StartTime.Format(time.DateTime),
		StopTime:  a.StopTime.Format(time.DateTime),
		Info:      a.Info,
		Rule:      a.Rule,
		Status:    int64(a.Status),
		JoinNum:   int64(a.JoinNum),
	}, nil
}

// 用户被帮助表列表
func (s *Serve) GetUserHelpList(_ context.Context, in *__.GetUserHelpListReq) (*__.GetUserHelpListResp, error) {
	list, err := dao.UserHelpList(in.ToHelpId)
	if err != nil {
		return nil, err
	}
	return &__.GetUserHelpListResp{List: list}, nil
}

// 用户参加活动列表
func (s *Serve) GetUserJoinActivityList(_ context.Context, in *__.GetUserJoinActivityListReq) (*__.GetUserJoinActivityListResp, error) {
	list, err := dao.GetUserJoinActivityList(in)
	if err != nil {
		return nil, err
	}
	return &__.GetUserJoinActivityListResp{List: list}, nil
}

// 用户帮助砍价列表
func (s *Serve) GetUserToHelpList(_ context.Context, in *__.GetUserToHelpListReq) (*__.GetUserToHelpListResp, error) {
	list, err := dao.GetUserToHelpList(in)
	if err != nil {
		return nil, err
	}
	return &__.GetUserToHelpListResp{List: list}, nil
}

// 修改砍价活动
func (s *Serve) UpdateCutActivity(_ context.Context, in *__.UpdateCutActivityReq) (*__.UpdateCutActivityResp, error) {
	err := dao.UpdateCutActivity(in)
	if err != nil {
		return nil, err
	}
	return &__.UpdateCutActivityResp{Id: in.Id}, nil
}

// 修改砍价商品
func (s *Serve) UpdateCutGoods(_ context.Context, in *__.UpdateCutGoodsReq) (*__.UpdateCutGoodsResp, error) {
	err := dao.UpdateCutGoods(in)
	if err != nil {
		return nil, err
	}
	return &__.UpdateCutGoodsResp{
		Id: in.Id,
	}, nil
}

// 参加的记录详情
func (s *Serve) JoinDetail(_ context.Context, in *__.JoinDetailReq) (*__.JoinDetailResp, error) {
	j, err := dao.JoinDetail(in)
	if err != nil {
		return nil, err
	}
	progress, err := dao.JoinProgress(j)
	if err != nil {
		return nil, err
	}
	return &__.JoinDetailResp{
		Id:          int64(j.Id),
		Uid:         int64(j.Uid),
		CutGoodsId:  int64(j.CutGoodsId),
		CutPriceMin: float32(j.CutPriceMin),
		CutPrice:    float32(j.CutPrice),
		IsCutPrice:  float32(j.CutPriceMin),
		Status:      int64(j.Status),
		Num:         int64(j.Num),
		Progress:    float32(progress),
	}, nil
}

// 删除砍价参与记录
func (s *Serve) JoinDel(_ context.Context, in *__.JoinDelReq) (*__.JoinDelResp, error) {
	var j models.CutUserJoin
	j.Id = uint32(in.Id)
	err := j.DelJoin()
	if err != nil {
		return nil, err
	}
	return &__.JoinDelResp{}, nil
}

// 删除砍价商品
func (s *Serve) CutGoodsDel(_ context.Context, in *__.CutGoodsDelReq) (*__.CutGoodsDelResp, error) {
	var g models.CutGoods
	g.Id = uint32(in.Id)
	err := g.DelCutGoods()
	if err != nil {
		return nil, err
	}
	return &__.CutGoodsDelResp{}, nil
}

// 删除砍价活动
func (s *Serve) CutActivityDel(_ context.Context, in *__.CutActivityDelReq) (*__.CutActivityResp, error) {
	var a models.CutActivity
	a.Id = uint32(in.Id)
	err := a.DelActivity()
	if err != nil {
		return nil, err
	}
	return &__.CutActivityResp{}, nil
}

// 记录列表
func (s *Serve) CutJoinList(_ context.Context, in *__.CutJoinListReq) (*__.CutJoinListResp, error) {
	list, err := dao.JoinList()
	if err != nil {
		return nil, err
	}
	return &__.CutJoinListResp{List: list}, nil
}

// 帮助列表
func (s *Serve) CutHelpList(_ context.Context, in *__.CutHelpListReq) (*__.CutHelpListResp, error) {
	list, err := dao.HelpList()
	if err != nil {
		return nil, err
	}
	return &__.CutHelpListResp{List: list}, nil
}
